/*!
 *@name     jquery.barrager.js
 *@version  1.1
 *@author   yaseng@uauc.net
 *@url      https://github.com/yaseng/jquery.barrager.js
 */
(function($) {

	$.fn.barrager = function(barrage) {
		barrage = $.extend({
			close:true,
			bottom: 0,
			max: 10,
			speed: 8,
			color: '#fff',
			old_ie_color : '#000000'
		}, barrage || {});

		var time = new Date().getTime();
		var barrager_id = 'barrage_' + time;
		var id = '#' + barrager_id;
		var div_barrager = $("<div class='barrage' id='" + barrager_id + "'></div>").appendTo($(this));
		var window_height = $(window).height() - 100;
		var this_height =  (window_height > this.height()) ? this.height() : window_height;
		var window_width = $(window).width() + 500;
		var this_width =  (window_width > this.width()) ? this.width() : window_width;
		var bottom = (barrage.bottom == 0) ? Math.floor(Math.random() * this_height + 40) : barrage.bottom;
		div_barrager.css("bottom", bottom + "px");
		div_barrager_box = $("<div class='barrage_box cl'></div>").appendTo(div_barrager);
		if(barrage.img){

			div_barrager_box.append("<a class='portrait z' href='javascript:;'></a>");
			var img = $("<img src='' >").appendTo(id + " .barrage_box .portrait");
			img.attr('src', barrage.img);
		}
		div_barrager_box.append(" <div class='z p'></div>");
		if(barrage.close){

			div_barrager_box.append(" <div class='close z'></div>");

		}
		
		var content = $("<a title='' href='' target='_blank'></a>").appendTo(id + " .barrage_box .p");
		content.attr({
			'href': barrage.href,
			'id': barrage.id
		}).empty().append(barrage.info);
		if(navigator.userAgent.indexOf("MSIE 6.0")>0  ||  navigator.userAgent.indexOf("MSIE 7.0")>0 ||  navigator.userAgent.indexOf("MSIE 8.0")>0  ){

			content.css('color', barrage.old_ie_color);

		}else{

			content.css('color', barrage.color);

		}
		
		var i = 0;
		div_barrager.css('margin-right', 0);
		
 		$(id).animate({right:this_width},barrage.speed*1000,function(){

			$(id).remove();
		});

		div_barrager_box.mouseover(function() {
		     $(id).stop(true);
		});

		div_barrager_box.mouseout(function() {

			$(id).animate({right:this_width},barrage.speed*1000,function(){

				$(id).remove();
			});

 		});

		$(id+'.barrage .barrage_box .close').click(function(){

			$(id).remove();

		})

	}
 
	$.fn.barrager.removeAll=function(){

		 $('.barrage').remove();

	}

})(jQuery);


function tanmu(url) {
	  //获取
     $.getJSON(url,function(data){
      //是否有数据
	  if(typeof(data) != 'object'){
        return  false;
      }
      var looper_time=data.looper_time;
      var items=data.items;
      var total=items.length;
      var run_once=true;
      var index=0;
      barrager();
      function  barrager(){
	    if(run_once){
            looper=setInterval(barrager,looper_time);                
            run_once=false;
        }
		$('.player_zanpian').barrager(items[index]);
		  
        index++;
        if(index == total){
            clearInterval(looper);
            return false;
        }   
      }

  });
	
}

function tanmuone(url) {
	  //获取
     $.getJSON(url,function(data){
      //是否有数据
	  if(typeof(data) != 'object'){
        return  false;
      }
      var looper_time=data.looper_time;
      var items=data.items;
      var total=items.length;
      var run_once=true;
      var index=0;
      barrager();
      function  barrager(){
		$('.player_zanpian').barrager(items[index]); 
      }

  });
	
}

$(document).ready(function(){	
$("#tammucomm").click(function(e) {
	$("#tamuform").qiresub({
		curobj : $("#tanmucomm"),
		txt : '数据提交中,请稍后...',
		onsucc : function(result) {
			$.hidediv(result);
			if (parseInt(result['rcode']) > 0) {
				tanmuone(Root + "index.php?s=home-barrage-index-t-1-id-"+ Id + '') ;
			}
		}
	}).post({
		url : Root + 'index.php?s=user-home-addcomm'
	});
	
	return false;
});


if (Id != undefined && Id != null && Id != '') {
	tanmu(Root +"index.php?s=home-barrage-index-id-" + Id + '')

};	
    $("#slider").toggle(
            function () {
                $(this).css("left","0");
				$(".m").toggleClass("on");
				$(".tanmu_m,.tamnu_open").toggleClass("one");
				$.fn.barrager.removeAll();
				clearInterval(looper);
                return false;
            },
            function () {
                $(this).css("left","44px");
				$(".m").toggleClass("on");
				$(".tanmu_m,.tamnu_open").toggleClass("one");
				tanmu(Root +"index.php?s=home-barrage-index-id-" + Id + '');
            }
        );
});	